package main

import (
	"github.com/gin-blog/models"
	"github.com/gin-blog/pkg/logging"
	"github.com/gin-blog/pkg/setting"
	"github.com/gin-blog/routers"
	"log"
	"net/http"
	"os"
	"os/signal"
	"context"
	"time"
	"fmt"
)

/**
https://github.com/EDDYCJY/go-gin-example
https://github.com/EDDYCJY/go-gin-example/blob/master/README_ZH.md
https://segmentfault.com/a/1190000013297625

运行：
go get github.com/pilu/fresh
cd ./gin-blog
fresh
*/
func main() {
	setting.Setup()
	models.Setup()
	logging.Setup()

	srv := &http.Server{
		Addr: fmt.Sprintf(":%d", setting.ServerSetting.HttpPort),
		Handler: routers.InitRouter(),
	}

	go func() {
		if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
			log.Fatalf("listen: %s\n", err)
		}
	}()

	quit := make(chan os.Signal)
	signal.Notify(quit, os.Interrupt)
	<-quit
	log.Println("Shutdown Server ...")

	ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
	defer cancel()
	if err := srv.Shutdown(ctx); err != nil {
		log.Fatal("Server Shutdown:", err)
	}
	log.Println("Server exiting")

}
